package com.jrwd.recycletest;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jrwd.recycletest.util.GlideUtil;
import com.jrwd.recycletest.util.SpUtils;
import com.jrwd.recycletest.util.StatisticsUtils;

import java.util.List;

/**
 * Created by ${templefck} on 2018/6/22.
 */
public class EntranceAdapter extends RecyclerView.Adapter<EntranceAdapter.EntranceViewHolder> {

    private List<HomeRes.DataBean.ProductStarBean> mDatas;
    private List<HomeRes.DataBean.ProductStarBean> homeEntrances;
    /**
     * 页数下标,从0开始(通俗讲第几页)
     */
    private int mIndex;

    /**
     * 每页显示最大条目个数
     */
    private int mPageSize;

    private Context mContext;
    private Activity mActivity;

    private final LayoutInflater mLayoutInflater;

    private RecyclerView recyclerView;

    public EntranceAdapter(Activity activity, Context context, List<HomeRes.DataBean.ProductStarBean> datas, int index, int pageSize) {
        this.mContext = context;
        this.mActivity = activity;
        this.homeEntrances = datas;
        mPageSize = pageSize;
        mDatas = datas;
        mIndex = index;
        mLayoutInflater = LayoutInflater.from(context);

    }

    @Override
    public EntranceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new EntranceViewHolder(mLayoutInflater.inflate(R.layout.item_home_entrance, null));
    }

    @Override
    public void onBindViewHolder(EntranceViewHolder holder, final int position) {
        /**
         * 在给View绑定显示的数据时，计算正确的position = position + mIndex * mPageSize，
         */
        final int pos = position + mIndex * mPageSize;
        holder.entranceTitleTextView.setText(homeEntrances.get(pos).getTitle());
        holder.entranceTipTextView.setText(homeEntrances.get(pos).getFeature());

        if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            //
            holder.entranceApplayTextView.setText(Html.fromHtml(mContext.getString(R.string.star_apply,homeEntrances.get(pos).getApply_count()), Html.FROM_HTML_MODE_COMPACT));
        } else {
            holder.entranceApplayTextView.setText(Html.fromHtml(mContext.getString(R.string.star_apply,homeEntrances.get(pos).getApply_count())));
        }

        GlideUtil.loadSmallRoundImage(mContext, homeEntrances.get(pos).getImg(), holder.entranceIconImageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                StatisticsUtils.tjPoisition(mContext, "6", pos);
                HomeRes.DataBean.ProductStarBean product = homeEntrances.get(pos);


                if (product.getType().equals("2")){
                    //api详情
                    boolean isLogin = (boolean) SpUtils.get(mContext, "login", false);
                    if (!isLogin){
//                        ToastUtils.showShort((Activity) mContext, "请先登录");
//                        ActivitiesManager.intentActivity((Activity) mContext, FirstMainAct.class, false);
                    }else {
//                        Intent intent = new Intent(mContext, ApiProDetailAct.class);
//                        intent.putExtra("product", product);
//                        intent.putExtra("productId", product.getId());
//                        intent.putExtra("position", position);
//                        intent.putExtra("type", "1");
//                        mContext.startActivity(intent);
                    }

                }else {
//                    //非api统一进详情在判断 联合登录
//                    Intent intent = new Intent(mContext, ProductDetailsAct.class);
//                    intent.putExtra("product", product);
//                    intent.putExtra("productId", product.getId());
//                    intent.putExtra("position", position);
//                    intent.putExtra("type", "1");
//                    intent.putExtra("eff_time", product.getEff_time());
//                    intent.putExtra("product_type", "1");
//                    SpUtils.put(mContext, "lable_category", 12);
//                    mContext.startActivity(intent);
                }


            }
        });
    }



    @Override
    public int getItemCount() {
        return mDatas.size() > (mIndex + 1) * mPageSize ? mPageSize : (mDatas.size() - mIndex * mPageSize);
    }

    @Override
    public long getItemId(int position) {
        return position + mIndex * mPageSize;
    }

    class EntranceViewHolder extends RecyclerView.ViewHolder {

        private TextView entranceTitleTextView;
        private TextView entranceTipTextView;
        private TextView entranceApplayTextView;
        private ImageView entranceIconImageView;

        public EntranceViewHolder(View itemView) {
            super(itemView);
            entranceIconImageView = (ImageView) itemView.findViewById(R.id.iv_title);
            entranceTitleTextView = (TextView) itemView.findViewById(R.id.tv_title);
            entranceTipTextView = (TextView) itemView.findViewById(R.id.tv_tip);
            entranceApplayTextView = (TextView) itemView.findViewById(R.id.tv_apply);

        }
    }
}
